package com.lsk.util;

import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 *
 * @param <T>
 */
public class PageUtil<T> {
    private int currentPage;//当前页
    private int size;//每页个数
    private List<T> list;//分页中泛型类
    private int totalRows;//总页数
    private int startIndex;//开始脚表
    private String sql;//sql语句
    private String countSql;//统计Sql

    public PageUtil(int currentPage, int size, String sql){
        this.currentPage=currentPage<2?1:currentPage;
        this.setStartIndex((this.currentPage-1)*size);
        this.sql=" select count(*) from ( "+sql+" )  A"+UtilUUID.getUUID()+"";
        this.totalRows= Db.queryInt(this.sql);
        this.list= (List<T>) Db.find(sql+" limit "+this.getStartIndex()+","+size);

   }
    public PageUtil(int currentPage, int size, String sql,String countSql){
        this.currentPage=currentPage<2?1:currentPage;
        this.setStartIndex((this.currentPage-1)*size);
        this.sql=sql;
        this.countSql=countSql;
        this.totalRows= Db.queryInt(this.countSql);
        this.list= (List<T>) Db.find(sql+" limit "+this.getStartIndex()+","+size);

    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

}
